using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace HREDU.Hoang_Tuan.Classes
{
    public class CUser
    {
        private int _user_id;
        private string _loginname;
        private string _pass;
        private string _fullname;
        private int _ban_id;
        private bool _isadmin;
        private bool _ishide;
        private string _ghichu;

        public int User_ID
        {
            get { return _user_id; }
            set { _user_id = value; }
        }

        public string LoginName
        {
            get { return _loginname; }
            set { _loginname = value; }
        }

        public string FullName
        {
            get { return _fullname; }
            set { _fullname = value; }
        }

        public string Pass
        {
            get { return _pass; }
            set { _pass = value; }
        }

        public int Ban_ID
        {
            get { return _ban_id; }
            set { _ban_id = value; }
        }

        public bool IsAdmin
        {
            get { return _isadmin; }
            set { _isadmin = value; }
        }

        public bool IsHide
        {
            get { return _ishide; }
            set { _ishide = value; }
        }

        public string GhiChu
        {
            get { return _ghichu; }
            set { _ghichu = value; }
        }

        public override string ToString()
        {
            return _loginname;
        }
    }

    public class CUsers
    {
        //public static string GetGiaTriThanhPhan(string sConnString, string pName)
        //{
        //    string KQ = "";
        //    string strTP = "";
        //    string[] data = sConnString.Split(new Char[]{';'});
        //    for (int i = 0; i <= data.GetUpperBound(0); i++)
        //    {
        //        if (data[i].IndexOf(pName) > -1)
        //        {
        //            strTP = data[i];
        //            break; // TODO: might not be correct. Was : Exit For
        //        }
        //    }
        //    string[] data2 = strTP.Split(new Char[]{'='});
        //    if (data2.GetUpperBound(0) > 0)
        //    {
        //        KQ = data2[1];
        //    }
        //    return KQ;
        //}

        public static bool DuocPhepChay(string pLoginName, string pPass)
        {
            bool KQ = false;
            string strSQL = null;
            int User_ID = -1;
            try
            {
                strSQL = "SELECT user_id FROM NGUOIDUNG" + " WHERE loginname = '" + pLoginName + "' and pass = cast('" + pPass + "' as varbinary(30))";
                User_ID = int.Parse(MITI.BaseDB.Instance.ExecSql_DataValue(strSQL).ToString());
                if (User_ID > 0)
                    KQ = true;
            }
            catch (System.Exception ex)
            {
                KQ = false;
            }
            return KQ;
        }

        public static CUser GetUserByName(string pLoginName)
        {
            CUser objUser = new CUser();
            string strSQL = null;

            strSQL = "SELECT user_id, loginname, cast(pass as varchar(30)) as pass, fullname, ban_id, isadmin, ishide, ghichu FROM NGUOIDUNG" + " WHERE loginname = '" + pLoginName + "'";

            SqlDataReader objReader = null;
            try
            {
                objReader = MITI.BaseDB.Instance.ExecSql_DataReader(strSQL);
                while (objReader.Read())
                {
                    objUser = GetObjectByDataReader(objReader);
                }
                if ((objReader != null))
                {
                    objReader.Close();
                }
                MITI.BaseDB.Instance.CloseConnect();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return objUser;
        }

        //Kiem tra xem co quyen admin cua SQL Server luon khong
        public static bool isSysAdmin(string pUserName)
        {
            bool KQ = false;
            try
            {
                string strIsAdmin = "select IS_SRVROLEMEMBER('sysadmin','" + pUserName + "')";
                int value = int.Parse(MITI.BaseDB.Instance.ExecSql_DataValue(strIsAdmin).ToString());
                if (value == 1)
                {
                    KQ = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return KQ;
        }

        //Kiem tra xem co quyen admin cua chuong trinh hay khong
        public static bool isAdmin(string pUserName)
        {
            bool KQ = false;
            try
            {
                string strIsAdmin = "select isAdmin from NGUOIDUNG where loginname = N'" + pUserName + "'";
                KQ = bool.Parse(MITI.BaseDB.Instance.ExecSql_DataValue(strIsAdmin).ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return KQ;
        }

        public static CUser GetObjectByDataReader(SqlDataReader objReader)
        {
            CUser objUser = new CUser();
            //objDonVi.ID_DONVI = objReader.GetInt32(0);
            //objDonVi.NHOM = objReader.GetString(1);
            //objDonVi.CUOI = objReader.GetBoolean(2);
            //objDonVi.TEN_DONVI = objReader.GetString(3);
            //objDonVi.DIENTHOAI = (objReader.GetValue(4) == DBNull.Value ? null : objReader.GetString(4));
            //objDonVi.FAX = (objReader.GetValue(5) == DBNull.Value ? null : objReader.GetString(5));
            //objDonVi.DIACHI = (objReader.GetValue(6) == DBNull.Value ? null : objReader.GetString(6));

            objUser.User_ID = objReader.GetInt16(0);
            objUser.LoginName = objReader.GetString(1);
            objUser.Pass = objReader.GetString(2);
            objUser.FullName = objReader.GetString(3);
            objUser.Ban_ID = objReader.GetInt32(4);
            objUser.IsAdmin = objReader.GetBoolean(5);
            objUser.IsHide = objReader.GetBoolean(6);
            objUser.GhiChu = (objReader.GetValue(7) == DBNull.Value ? null : objReader.GetString(7));
            return objUser;
        }
    }

}
